
function BaseController(args) {
	this.contextPath = args["contextPath"];
	this.idName = args["idName"];
	this.tableName = args["tableName"];
	this.pid = args["pid"];
}

BaseController.prototype = {
	getContextPath:function() {
		return this.contextPath;
	},
	getTableName:function() {
		return this.tableName;
	},
	getPid:function() {
		return this.pid;
	},
	//是否为空
	isNull:function(obj){
		if("undefined" == obj || null == obj || "" == obj ){
			return true;
		}
		return false;
	},
	//选择所有
	selectAll:function(thisObj){
		var fidArr = document.getElementsByName(this.idName);
		if(thisObj.checked == true){
			for(var i = 0;i<fidArr.length;i++){
				fidArr[i].checked = true;
			}
		}else{
			for(var i = 0;i<fidArr.length;i++){
				fidArr[i].checked = false;
			}
		}
	},
	getSelectCheckBoxArr:function() {
		var pkArr = document.getElementsByName(this.idName);
	    var len = pkArr.length;
	    var selectObjArr = new Array();
	    for (var i = 0; i < len; i++) {
	        if (pkArr[i].checked == true) {
	        	selectObjArr[selectObjArr.length] = pkArr[i];
	        }
	    }
	    return selectObjArr;
	},
	getSelectId:function() {
		var pkArr = document.getElementsByName(this.idName);
	    return pkArr[0].value;
	},
	getSelectIds:function() {
		var selectIds = "";
		var pkArr = document.getElementsByName(this.idName);
	    var len = pkArr.length;
	    var selectObjArr = new Array();
	    for (var i = 0; i < len; i++) {
	        if (pkArr[i].checked == true) {
	        	selectIds += "," + pkArr[i].value;
	        }
	    }
	    return selectIds.substring(1);
	},
	checkSelect:function(flag){
		var selectObjArr = this.getSelectCheckBoxArr();
		var len = selectObjArr.length;
		if(flag == "single"){
			if(len != 1){
				alert("请选择单条记录！");
				return false;
			}
		}else{//more
			if(len < 1){
				alert("你没有选择任何记录！");
				return false;
			}
		}
		return true;
	},
	//到新增页面
	toAdd:function(){
		
	},
	//到修改页面
	toEdit:function(url){
		var selectObjArr = this.getSelectCheckBoxArr();
		var len = selectObjArr.length;
		if(len!=1){
			alert("请选择一条记录");
			return;
		}
		if(url.indexOf('?') == -1) {
			url += "?";
		} else {
			url += "&"
		}
		document.location.href = url + "id=" + selectObjArr[0].value + "&tableName=" + this.tableName;
	},
	//删除
	doDel:function(url){
		if(this.checkSelect() == true){
			if(url.indexOf('?') == -1) {
				url += "?";
			} else {
				url += "&"
			}
			
			var form = document.forms[0];
			form.action = url + "operate=del";
			form.submit();
		}
	},
	//保存
	doSave:function(url) {
		document.getElementById("operate").value = "save";
		var form = document.forms[0];
		form.action = url + "?operate=save";
		form.submit();
	},
	//选择
	doSelect:function(flag) {
		if(this.checkSelect(flag)) {
			var selectArr = [];
			var selectObjArr = this.getSelectCheckBoxArr();
			var len = selectObjArr.length;
			for(var i = 0;i<len;i++) {
				var selectData = {};
				var selectObj = selectObjArr[i];
				selectData["ID"] = selectObj.value;
				var trObj = selectObj.parentNode.parentNode;
				var tdLen = trObj.cells.length;
				for(var j = 0;j<tdLen;j++) {
					var tdObj = trObj.cells[j];
					var name = tdObj.getAttribute("name");
					var value = tdObj.getAttribute("value");
					if(!this.isNull(name)) {
						selectData[name] = value;
					}
				}
				selectArr.push(selectData);
			}
			
			window.opener.doAfterSelect(selectArr);//调用父页面的doAfterSelect方法
			window.close();//关闭窗口
		}
	},
	/**
 	* name:窗口的名称，如果打开多个相同名字的名称，后一个窗口会覆盖掉前面的窗口
 	* scrollbars:默认为没有滚动条(yes为有滚动条)
 	*/
	openWindow:function(url,width,height,name,scrollbars){
		var _height = (height!=null) ? height : "400";
		var	_width = (width != null) ? width : "600";
		var topHeight=(window.screen.height-_height)/2;
		var leftWidth=(window.screen.width-_width)/2;

		var _scrollbars = (scrollbars!=null) ? scrollbars : "no";
		var _name = (name!=null) ? name : "select";
		var win = window.open(url,_name,'height='+_height+',width='+_width+',top='+topHeight+',left='+leftWidth+',alwaysRaised=yes,depended=yes,titlebar=yes,toolbar=no,menubar=no,scrollbars='+_scrollbars+', resizable=yes,location=no, status=no,z-look=yes');		
		/*
		try{
			window.top.sub_windows.push(win);
		} catch(e){
			try{
				window.opener.top.sub_windows.push(win);
			} catch(e){  }
		}*/
		return win;
	},
	//打开模态窗口
	openModalWindow:function(url,width,height){
		window[this.id] = this;
		var args=[window,this];	
		
		var dialogWidth = width;
		var dialogHeight = parseInt(height);
		var isIe7 = /msie 7\.0/i.test(navigator.userAgent);//是否IE7
		if(isIe7){
			dialogHeight = dialogHeight - 40;
		}
		var sFeatures = "dialogHeight:"+dialogHeight+"px;dialogWidth:"+dialogWidth+"px;center: Yes; help: no; resizable: no; status: no;scroll:no";
		var returnValue = showModalDialog(url, args ,sFeatures);//需要将当前窗口作为参数传过去

	}
}